package mapEditor.view
{
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	import flash.ui.Mouse;
	import flash.ui.MouseCursor;
	
	import mapEditor.MapEditorManager;
	import mapEditor.events.MapEditorEvent;
	import mapEditor.view.assetEdit.AssetAreaEditView;
	import mapEditor.view.mapEdit.MapTerrainEditView;
	import mapEditor.view.resItemEdit.ItemEditView;
	import mapEditor.vo.ModuleType;
	import mapEditor.vo.ToolType;
	
	import utils.DisplayUtil;
	import utils.greensock.TweenLite;
	import utils.greensock.easing.Back;
	import utils.greensock.plugins.TransformAroundPointPlugin;
	import utils.greensock.plugins.TweenPlugin;

	public class EditAreaView extends Sprite
	{
		/**
		 *编辑区类型:1=资源编辑 
		 */		
		private var _areaType:int;
		/**
		 *编辑区大小 
		 */		
		private var _areaRec:Rectangle=new Rectangle();
		/**
		 *资源编辑 
		 */		
		private var _assetAreaEditView:AssetAreaEditView;
		/**
		 *物件项编辑 
		 */		
		private var _itemEditView:ItemEditView;
		/**
		 *地形编辑 
		 */		
		private var _mapTerrainEditView:MapTerrainEditView;
		
		public function EditAreaView()
		{
			TweenPlugin.activate([TransformAroundPointPlugin]);
			addEvent();
		}
		
		private function addEvent():void
		{
			MapEditorManager.instance.mapEditorModel.addEventListener(MapEditorEvent.RES_MODULE_TYPE_CHANGE, __onResModuleTypeChange);
			MapEditorManager.instance.mapEditorModel.addEventListener(MapEditorEvent.SCENE_MODULE_TYPE_CHANGE, __onSceneModuleTypeChange);
			addEventListener(MouseEvent.MOUSE_OVER, __onMouseOver);
			addEventListener(MouseEvent.MOUSE_DOWN, __onMouseDown);
			addEventListener(MouseEvent.MOUSE_UP, __onMouseUp);
			addEventListener(MouseEvent.MOUSE_OUT, __onMouseOut);
		}
		
		private function removeEvent():void
		{
			MapEditorManager.instance.mapEditorModel.removeEventListener(MapEditorEvent.RES_MODULE_TYPE_CHANGE, __onResModuleTypeChange);
			MapEditorManager.instance.mapEditorModel.removeEventListener(MapEditorEvent.SCENE_MODULE_TYPE_CHANGE, __onSceneModuleTypeChange);
			removeEventListener(MouseEvent.MOUSE_DOWN, __onMouseDown);
			removeEventListener(MouseEvent.MOUSE_UP, __onMouseUp);
			removeEventListener(MouseEvent.MOUSE_OVER, __onMouseOver);
			removeEventListener(MouseEvent.MOUSE_OUT, __onMouseOut);
		}
		
		private function __onResModuleTypeChange(evt:MapEditorEvent):void
		{
			show();
		}
		private function __onSceneModuleTypeChange(evt:MapEditorEvent):void
		{
			show();
		}
		
		private function __onMouseOver(evt:MouseEvent):void
		{
			switch(MapEditorManager.instance.mapEditorModel.toolType)
			{
				case ToolType.TOOL_MOVE:
					Mouse.cursor=MouseCursor.HAND;
					break;
			}
		}
		
		private function __onMouseOut(evt:MouseEvent):void
		{
			switch(MapEditorManager.instance.mapEditorModel.toolType)
			{
				case ToolType.TOOL_MOVE:
					Mouse.cursor=MouseCursor.AUTO;
					break;
			}
		}
		
		private function __onMouseDown(evt:MouseEvent):void
		{
			switch(MapEditorManager.instance.mapEditorModel.toolType)
			{
				case ToolType.TOOL_MOVE:
					startDrag();
					break;
				case ToolType.TOOL_ZOOM_IN:
					TweenLite.to(this, 1, {transformAroundPoint:{point:new Point(evt.localX,evt.localY), scaleX:this.scaleX+0.5, scaleY:this.scaleX+0.5}, ease:Back.easeOut});
					evt.stopImmediatePropagation();
					break;
				case ToolType.TOOL_ZOOM_OUT:
					TweenLite.to(this, 1, {transformAroundPoint:{point:new Point(evt.localX,evt.localY), scaleX:this.scaleX-0.5, scaleY:this.scaleX-0.5}, ease:Back.easeOut});
					evt.stopImmediatePropagation();
					break;
			}
		}
		
		private function __onMouseUp(evt:MouseEvent):void
		{
			stopDrag();
		}
		
		public function show():void
		{
			DisplayUtil.dispose(_assetAreaEditView);
			_assetAreaEditView=null;
			DisplayUtil.dispose(_itemEditView);
			_itemEditView=null;			
			DisplayUtil.dispose(_mapTerrainEditView);
			_mapTerrainEditView=null;	
			
			switch(MapEditorManager.instance.mapEditorModel.moduleType)
			{
				case ModuleType.RES_EDIT:
					switch(MapEditorManager.instance.mapEditorModel.resModuleType)
					{
						case ModuleType.RES_ASSET_EDIT:
							_assetAreaEditView=new AssetAreaEditView(_areaRec);
							_assetAreaEditView.x=_areaRec.width/2;
							_assetAreaEditView.y=_areaRec.height/2;
							addChild(_assetAreaEditView);
							break;
						case ModuleType.RES_ITEM_EDIT:
							_itemEditView=new ItemEditView(_areaRec);
							addChild(_itemEditView);
							break;
					}
				break;
				case ModuleType.SCENE_EDIT:
					switch(MapEditorManager.instance.mapEditorModel.sceneModuleType)
					{
						case ModuleType.SCENE_TERRAIN_EDIT:
							_mapTerrainEditView=new MapTerrainEditView(_areaRec);
							addChild(_mapTerrainEditView);
							break;
					}
					break;
			}
		}
		
		public function resize():void
		{
			switch(MapEditorManager.instance.mapEditorModel.moduleType)
			{
				case ModuleType.RES_EDIT:
					switch(MapEditorManager.instance.mapEditorModel.resModuleType)
					{
						case ModuleType.RES_ASSET_EDIT:
							_assetAreaEditView.x=_areaRec.width/2;
							_assetAreaEditView.y=_areaRec.height/2;
							_assetAreaEditView.resize();
							break;
						case ModuleType.RES_ITEM_EDIT:
							_itemEditView.resize();
							break;
					}
					break;
				case ModuleType.SCENE_EDIT:
					switch(MapEditorManager.instance.mapEditorModel.sceneModuleType)
					{
						case ModuleType.SCENE_TERRAIN_EDIT:
							_mapTerrainEditView.resize();
							break;
					}
					break;
			}
			drawEditArea();
		}
		
		private function drawEditArea():void
		{
			graphics.clear();
			graphics.beginFill(0xffffff);
			graphics.drawRect(0,0,_areaRec.width, _areaRec.height);
			graphics.endFill();
		}
		
		public function dispose():void
		{
			removeEvent();
			_areaRec=null;
			if(_assetAreaEditView)
			{
				_assetAreaEditView.dispose();
				_assetAreaEditView=null;
			}
			if(_itemEditView)
			{
				_itemEditView.dispose();
				_itemEditView=null;
			}
			if(_mapTerrainEditView)
			{
				_mapTerrainEditView.dispose();
				_mapTerrainEditView=null;
			}			
			if(parent)parent.removeChild(this);
		}

		/**
		 *编辑区大小 
		 */
		public function get areaRec():Rectangle
		{
			return _areaRec;
		}

		/**
		 * @private
		 */
		public function set areaRec(value:Rectangle):void
		{
			_areaRec = value;
		}

		/**
		 *编辑区类型:1=资源编辑 
		 */
		public function get areaType():int
		{
			return _areaType;
		}

		/**
		 * @private
		 */
		public function set areaType(value:int):void
		{
			_areaType = value;
		}

	}
}